package com.hspedu.collection_;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Collection Iterator迭代器
 * @author enomix
 * @date 2022-06-14 21:17
 */
public class CollectionIterator {
    public static void main(String[] args) {
        Collection col = new ArrayList();

        col.add(new Book("三国演义","罗贯中", 20));
        col.add(new Book("水浒传","施耐庵", 20));
        col.add(new Book("西游记","吴承恩", 20));


        //先得到col集合对应的迭代器
        Iterator iterator = col.iterator();

        while(iterator.hasNext()){//判断是否还有数据
            System.out.println(iterator.next());
        }

        //需要再次遍历,重置迭代器
        iterator = col.iterator();

        //快速生成 while => itit
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println("next = " + next);
        }


    }
}


class Book{
    private String name;
    private String author;
    private double price;

    public Book() {
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}